🔁 50+ Python String Methods & Examples

 

🔁 50+ Python String Methods & Examples 📝

1. lower() – Convert string to lowercase

"HELLO".lower() # Output: "hello"

2. upper() – Convert string to uppercase

"hello".upper() # Output: "HELLO"

3. strip() – Remove leading & trailing whitespace

" hello ".strip() # Output: "hello"

4. lstrip() – Remove leading spaces

" hello".lstrip() # Output: "hello"

5. rstrip() – Remove trailing spaces

"hello ".rstrip() # Output: "hello"

6. replace() – Replace a substring

"apple".replace("a", "A") # Output: "Apple"

7. split() – Split string into a list

"a,b,c".split(",") # Output: ['a','b','c']

8. rsplit() – Split from the right

"a,b,c".rsplit(",", 1) # Output: ['a,b','c']

9. splitlines() – Split string by lines

"Hello\nWorld".splitlines() # Output: ['Hello','World']

10. join() – Join list into string

" ".join(["I", "love", "Python"]) # Output: "I love Python"

11. find() – Find substring index (-1 if not found)

"hello".find("e") # Output: 1

12. rfind() – Find substring from the right

"hello".rfind("l") # Output: 3

13. index() – Find substring index (raises error if not found)

"hello".index("e") # Output: 1

14. rindex() – Find substring index from right

"hello".rindex("l") # Output: 3

15. count() – Count occurrences of a substring

"hello".count("l") # Output: 2

16. startswith() – Check string prefix

"Python".startswith("Py") # Output: True

17. endswith() – Check string suffix

"Python".endswith("on") # Output: True

18. isdigit() – Check if all characters are digits

"123".isdigit() # Output: True

19. isalpha() – Check if all characters are letters

"Python".isalpha() # Output: True

20. isalnum() – Check if alphanumeric

"Python123".isalnum() # Output: True

21. isupper() – Check if all letters are uppercase

"HELLO".isupper() # Output: True

22. islower() – Check if all letters are lowercase

"hello".islower() # Output: True

23. istitle() – Check if string is title cased

"Hello World".istitle() # Output: True

24. isspace() – Check if string is whitespace

" ".isspace() # Output: True

25. title() – Convert string to title case

"hello world".title() # Output: "Hello World"

26. capitalize() – Capitalize first letter

"python".capitalize() # Output: "Python"

27. swapcase() – Swap upper/lower case

"Hello".swapcase() # Output: "hELLO"

28. center() – Center string with padding

"Python".center(10, "*") # Output: "**Python**"

29. ljust() – Left justify with padding

"Python".ljust(10, "-") # Output: "Python----"

30. rjust() – Right justify with padding

"Python".rjust(10, "-") # Output: "----Python"

31. zfill() – Pad numeric string with zeros

"42".zfill(5) # Output: "00042"

32. partition() – Split string into tuple by separator

"hello world".partition(" ") # Output: ('hello', ' ', 'world')

33. rpartition() – Partition from right

"hello world".rpartition(" ") # Output: ('hello', ' ', 'world')

34. expandtabs() – Replace tabs with spaces

"Hello\tWorld".expandtabs(4) # Output: "Hello World"

35. encode() – Encode string to bytes

"hello".encode("utf-8") # Output: b'hello'

36. decode() – Decode bytes to string

b"hello".decode("utf-8") # Output: "hello"

37. format() – Insert values into string

"My name is {}".format("Alice") # Output: "My name is Alice"

38. format_map() – Format using a dictionary

"My name is {name}".format_map({"name": "Bob"}) # Output: "My name is Bob"

39. encode() – Encode string in specified encoding

"hello".encode("ascii") # Output: b'hello'

40. removeprefix() – Remove starting substring (Python 3.9+)

"PythonTutorial".removeprefix("Python") # Output: "Tutorial"

41. removesuffix() – Remove ending substring (Python 3.9+)

"PythonTutorial".removesuffix("Tutorial") # Output: "Python"

42. translate() – Replace characters using mapping

"hello".translate(str.maketrans("h","H")) # Output: "Hello"

43. maketrans() – Create translation mapping

str.maketrans("abc", "123") # Mapping {'a':'1','b':'2','c':'3'}

44. casefold() – Aggressive lowercasing for comparisons

"HELLO".casefold() # Output: "hello"

45. removeprefix() – Remove prefix string

"TestString".removeprefix("Test") # Output: "String"

46. removesuffix() – Remove suffix string

"TestString".removesuffix("String") # Output: "Test"

47. isascii() – Check if all chars are ASCII

"Python".isascii() # Output: True

48. partition() – Split string into three parts

"Hello World".partition(" ") # Output: ('Hello',' ','World')

49. rpartition() – Split string from right

"Hello World".rpartition(" ") # Output: ('Hello',' ','World')

50. removeprefix() – Remove specific prefix

"PythonLearn".removeprefix("Python") # Output: "Learn"

51. removesuffix() – Remove specific suffix

"PythonLearn".removesuffix("Learn") # Output: "Python"

52. expandtabs() – Convert tab to spaces

"Hello\tWorld".expandtabs(4) # Output: "Hello World"

💡 Tips:

  • Use strip(), lstrip(), rstrip() to clean input.

  • Combine split() & join() for powerful text processing.

  • Use format() or f-strings for dynamic strings.

  • Use translate() and maketrans() for bulk character replacements.

Comments

Popular posts from this blog

🔁 Top 30 Python List Methods You Should Know

Python Project 7: Read & Write JSON Files

✅ Python – Quick Start Guide for Beginners